import api from "@/api";
// import wx from "weixin-js-sdk";
export default {
  state: {
    wxData: uni.getStorageSync("wxData") ? uni.getStorageSync("wxData") : null,
    configData: uni.getStorageSync("configData") ? uni.getStorageSync("configData") : null
  },
  mutations: {
    setWechatData(state, data) {
      state.wxData = data;
      uni.setStorageSync("wxData", data);
    },
    setConfigData(state, data) {
      state.configData = data;
      uni.setStorageSync("configData", data);
    }
  },
  actions: {
    //微信配置
    getWxConfigData({ commit }) {
      return new Promise(reslove => {
        api.getWxConfigData().then(res => {
          commit("setConfigData", res);
          reslove();
        });
      });
    },
    setWechatData({ commit }, data) {
      return new Promise((reslove, reject) => {
        api
          .getOpenId(data)
          .then(res => {
            commit("setWechatData", res);
            reslove();
          })
          .catch(err => {
            reject(err);
          });
      });
    },
    wxAuthorization() {
      return new Promise((reslove, reject) => {
        let url = window.location.href.split("#")[0];
        api
          .jssdkAuthorization({ url })
          .then(res => {
            let { jsapiTicket, ...data } = res;
            wx.config({
              debug: false,
              ...data,
              jsApiList: ["chooseWXPay", "chooseImage", "updateAppMessageShareData", "updateTimelineShareData"]
            });
            wx.ready(() => {
              reslove();
            });
          })
          .catch(err => reject(err));
      });
    }
  },
  getters: {}
};
